<?php
/**
 * 清除所有缓存
 */

echo "=== 清除所有缓存 ===\n\n";

// 清除 runtime 目录下的所有缓存文件
$dirs = [
    __DIR__ . '/runtime/cache',
    __DIR__ . '/runtime/session',
    __DIR__ . '/runtime/temp',
    __DIR__ . '/runtime/log',
];

$totalDeleted = 0;

foreach ($dirs as $dir) {
    if (!is_dir($dir)) {
        echo "目录不存在: {$dir}\n";
        continue;
    }
    
    echo "清理目录: {$dir}\n";
    
    $files = glob($dir . '/*');
    $count = 0;
    
    foreach ($files as $file) {
        if (is_file($file)) {
            unlink($file);
            $count++;
        }
    }
    
    echo "  删除 {$count} 个文件\n";
    $totalDeleted += $count;
}

echo "\n总共删除 {$totalDeleted} 个缓存文件\n";

// 验证菜单结构
echo "\n=== 验证菜单结构 ===\n";

$host = '113.44.136.17';
$port = 3306;
$database = 'qiandao';
$username = 'qiandao';
$password = 'NGbks3HWx5PhSxDX';

try {
    $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // 检查权限管理的结构
    $stmt = $pdo->query("
        SELECT id, pid, title, url
        FROM system_menu
        WHERE (id = 9 OR pid = 9 OR pid IN (SELECT id FROM system_menu WHERE pid = 9))
        AND status = 1
        ORDER BY 
            CASE WHEN id = 9 THEN 0 WHEN pid = 9 THEN 1 ELSE 2 END,
            sort DESC, id ASC
    ");
    
    $menus = $stmt->fetchAll(PDO::FETCH_ASSOC);
    
    echo "\n权限管理菜单结构：\n";
    foreach ($menus as $menu) {
        if ($menu['pid'] == 0) {
            echo "{$menu['title']} (ID:{$menu['id']})\n";
        } elseif ($menu['pid'] == 9) {
            echo "  ├─ {$menu['title']} (ID:{$menu['id']}, URL:{$menu['url']})\n";
        } else {
            echo "  │  └─ {$menu['title']} (ID:{$menu['id']}, URL:{$menu['url']})\n";
        }
    }
    
    // 统计三级菜单数量
    $level3Count = 0;
    foreach ($menus as $menu) {
        if ($menu['pid'] > 9) {
            $level3Count++;
        }
    }
    
    echo "\n三级菜单数量: {$level3Count}\n";
    
    if ($level3Count > 0) {
        echo "✓ 权限管理有三级菜单，应该显示在左侧\n";
    } else {
        echo "❌ 权限管理没有三级菜单，会显示在顶部\n";
    }
    
} catch (PDOException $e) {
    echo "数据库错误: " . $e->getMessage() . "\n";
}

echo "\n=== 完成 ===\n";
echo "请执行以下操作：\n";
echo "1. 在后台点击右上角退出登录\n";
echo "2. 清除浏览器缓存（Ctrl+Shift+Delete）\n";
echo "3. 关闭浏览器\n";
echo "4. 重新打开浏览器并登录\n";

